#!/usr/bin/env bash

# 为每个 C++ 项目创建独立的 VSCode 工作空间配置
# 每个项目都有自己的 .vscode 目录和配置文件

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 项目配置
PROJECTS="cpp_grpc:gRPC聊天应用 cpp_basic:基础C++项目 cpp_opencv:OpenCV项目 cpp_qt:Qt项目"

# 获取项目名称列表
get_all_projects() {
    for item in $PROJECTS; do
        echo "${item%:*}"
    done
}

# 为 cpp_grpc 项目创建配置
setup_grpc_workspace() {
    local project_dir="cpp_grpc"
    echo -e "${BLUE}设置 $project_dir 独立工作空间...${NC}"
    
    mkdir -p "$project_dir/.vscode"
    
    # c_cpp_properties.json
    cat > "$project_dir/.vscode/c_cpp_properties.json" << 'EOF'
{
    "configurations": [
        {
            "name": "gRPC Project",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include",
                "${workspaceFolder}/build",
                "/opt/homebrew/Cellar/grpc/1.74.0/include",
                "/opt/homebrew/Cellar/abseil/20240722.1/include",
                "/opt/homebrew/Cellar/c-ares/1.34.5/include",
                "/opt/homebrew/Cellar/openssl@3/3.5.1/include",
                "/opt/homebrew/Cellar/re2/20250805/include",
                "/opt/homebrew/Cellar/protobuf/29.3/include"
            ],
            "defines": [
                "_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS=1"
            ],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64",
            "compileCommands": "${workspaceFolder}/compile_commands.json"
        }
    ],
    "version": 4
}
EOF

    # launch.json
    cat > "$project_dir/.vscode/launch.json" << 'EOF'
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Server",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/chat_server",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "Build Project"
        },
        {
            "name": "Debug Client",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/chat_client",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "Build Project"
        }
    ]
}
EOF

    # tasks.json
    cat > "$project_dir/.vscode/tasks.json" << 'EOF'
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Project",
            "type": "shell",
            "command": "./build.sh",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": ["$gcc"],
            "detail": "Build the gRPC chat application"
        },
        {
            "label": "Run Server",
            "type": "shell",
            "command": "./build/chat_server",
            "group": "test",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "new"
            },
            "detail": "Run the gRPC chat server"
        },
        {
            "label": "Run Client",
            "type": "shell",
            "command": "./build/chat_client",
            "group": "test",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "new"
            },
            "detail": "Run the gRPC chat client"
        },
        {
            "label": "Clean Build",
            "type": "shell",
            "command": "rm -rf build/*",
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "detail": "Clean build artifacts"
        }
    ]
}
EOF

    # settings.json
    cat > "$project_dir/.vscode/settings.json" << 'EOF'
{
    "files.associations": {
        "*.proto": "proto3"
    },
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
    "cmake.configureOnOpen": false
}
EOF

    echo -e "${GREEN}✓ $project_dir 工作空间配置完成${NC}"
}

# 为 cpp_basic 项目创建配置
setup_basic_workspace() {
    local project_dir="cpp_basic"
    echo -e "${BLUE}设置 $project_dir 独立工作空间...${NC}"
    
    mkdir -p "$project_dir/.vscode"
    
    # c_cpp_properties.json
    cat > "$project_dir/.vscode/c_cpp_properties.json" << 'EOF'
{
    "configurations": [
        {
            "name": "Basic C++ Project",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include",
                "${workspaceFolder}/build"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}
EOF

    # launch.json
    cat > "$project_dir/.vscode/launch.json" << 'EOF'
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Main",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/cpp_basic",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "Build Project"
        }
    ]
}
EOF

    # tasks.json
    cat > "$project_dir/.vscode/tasks.json" << 'EOF'
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Project",
            "type": "shell",
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": ["$gcc"],
            "detail": "Build the C++ project"
        },
        {
            "label": "Run Program",
            "type": "shell",
            "command": "make run",
            "group": "test",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "new"
            },
            "detail": "Run the compiled program"
        },
        {
            "label": "Clean Build",
            "type": "shell",
            "command": "make clean",
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "detail": "Clean build artifacts"
        }
    ]
}
EOF

    # settings.json
    cat > "$project_dir/.vscode/settings.json" << 'EOF'
{
    "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools"
}
EOF

    echo -e "${GREEN}✓ $project_dir 工作空间配置完成${NC}"
}

# 为 OpenCV 项目创建配置模板
setup_opencv_workspace() {
    local project_dir="cpp_opencv"
    if [ ! -d "$project_dir" ]; then
        echo -e "${YELLOW}$project_dir 目录不存在，跳过...${NC}"
        return
    fi
    
    echo -e "${BLUE}设置 $project_dir 独立工作空间...${NC}"
    
    mkdir -p "$project_dir/.vscode"
    
    # c_cpp_properties.json
    cat > "$project_dir/.vscode/c_cpp_properties.json" << 'EOF'
{
    "configurations": [
        {
            "name": "OpenCV Project",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include",
                "${workspaceFolder}/build",
                "/opt/homebrew/Cellar/opencv/4.10.0_12/include/opencv4"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}
EOF

    echo -e "${GREEN}✓ $project_dir 工作空间配置完成${NC}"
}

# 创建工作空间启动脚本
create_workspace_launcher() {
    cat > "open_workspace.sh" << 'EOF'
#!/usr/bin/env bash

# 工作空间启动器 - 选择要打开的项目

echo "选择要打开的项目工作空间："
echo "1) cpp_grpc - gRPC 聊天应用"
echo "2) cpp_basic - 基础 C++ 项目"
echo "3) cpp_opencv - OpenCV 项目"
echo "4) cpp_qt - Qt 项目"
echo "5) 全部项目 (多窗口模式)"
echo "0) 退出"

read -p "请输入选择 (0-5): " choice

case $choice in
    1)
        echo "打开 cpp_grpc 项目..."
        code cpp_grpc
        ;;
    2)
        echo "打开 cpp_basic 项目..."
        code cpp_basic
        ;;
    3)
        echo "打开 cpp_opencv 项目..."
        code cpp_opencv
        ;;
    4)
        echo "打开 cpp_qt 项目..."
        code cpp_qt
        ;;
    5)
        echo "打开所有项目..."
        for project in cpp_grpc cpp_basic cpp_opencv cpp_qt; do
            if [ -d "$project" ]; then
                echo "打开 $project..."
                code "$project"
                sleep 1
            fi
        done
        ;;
    0)
        echo "退出"
        exit 0
        ;;
    *)
        echo "无效选择"
        exit 1
        ;;
esac
EOF

    chmod +x "open_workspace.sh"
    echo -e "${GREEN}✓ 工作空间启动器创建完成${NC}"
}

# 主函数
main() {
    echo -e "${BLUE}=== C++ 项目独立工作空间设置 ===${NC}"
    echo "这将为每个项目创建独立的 .vscode 配置目录"
    echo ""
    
    # 设置各个项目的工作空间
    setup_grpc_workspace
    setup_basic_workspace
    setup_opencv_workspace
    
    # 创建启动器
    create_workspace_launcher
    
    echo ""
    echo -e "${GREEN}=== 设置完成 ===${NC}"
    echo "现在每个项目都有独立的 VSCode 工作空间配置："
    echo ""
    echo "使用方法："
    echo "1. 运行 ./open_workspace.sh 选择要打开的项目"
    echo "2. 或直接使用: code cpp_grpc (打开指定项目)"
    echo "3. 或直接在项目目录中打开 VSCode"
    echo ""
    echo "每个项目现在都有："
    echo "- 独立的 IntelliSense 配置"
    echo "- 专门的调试设置"
    echo "- 项目特定的构建任务"
    echo "- 完全隔离的开发环境"
}

# 运行主函数
main "$@"